package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 公告控制器 - 处理系统公告管理相关的HTTP请求
 * 包括公告的增删改查、状态管理、前台展示等功能
 */
@RestController  // REST控制器，返回JSON数据
@RequestMapping("/api/notices")  // 公告API路径前缀
//@CrossOrigin  // 允许跨域访问
@Tag(name = "公告管理接口", description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")  // Swagger API分组
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @Operation(summary = "获取所有公告")
    @GetMapping("/list")
    public Result<List<Notice>> list() {
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //按照排序字段排序
        noticeLambdaQueryWrapper.orderByAsc(Notice::getPriority);

        //调用NoticeService中的list方法
        List<Notice> list = noticeService.list(noticeLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取所有有效的公告")
    @GetMapping("/active")
    public Result<List<Notice>> getActiveNotices() {

        List<Notice> list = noticeService.list(new LambdaQueryWrapper<Notice>().eq(Notice::getIsActive, true));
        return Result.success(list);
    }

    @Operation(summary = "获取最新公告")
    @GetMapping("/latest")
    public Result<List<Notice>> getLatestNotices(@Parameter(description = "最新公告数量") @RequestParam(defaultValue = "5") int limit) {
        //构建查询条件：有效公告+按更新时间倒序
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<Notice>()
                .eq(Notice::getIsActive, true)
                .orderByDesc(Notice::getUpdateTime);

        List<Notice> list = noticeService.list(queryWrapper.last("LIMIT " + limit));
        return Result.success(list);
    }

    @Operation(summary = "添加公告")
    @PostMapping("/add")
    public Result<String> addNotice(@RequestBody Notice notice) {
        boolean b = noticeService.save(notice);
        if (!b) {
            return Result.error("添加失败");
        } else {
            return Result.success("添加成功");
        }
    }

    @Operation(summary = "根据id获取公告详情")
    @GetMapping("/{id}")
    public Result<Notice> getNoticeContent(@PathVariable Long id) {
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }

    @Operation(summary = "更新公告")
    @PutMapping("/update")
    public Result<String> updateNotice(@RequestBody Notice notice) {
        boolean b = noticeService.updateById(notice);
        if (!b) {
            return Result.error("更新失败");
        } else {
            return Result.success("更新成功");
        }
    }

    @Operation(summary = "删除公告")
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteNotice(@PathVariable Long id) {
        boolean b = noticeService.removeById(id);
        if (!b) {
            return Result.error("删除失败");
        } else {
            return Result.success("删除成功");
        }
    }

    @Operation(summary = "切换公告状态")
    @PutMapping("/toggle/{id}")
    public Result toggle(@PathVariable Long id, @RequestParam boolean isActive) {
        //创建LambdaUpdateWrapper对象
        LambdaUpdateWrapper<Notice> noticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //设置要更新的字段
        noticeLambdaUpdateWrapper.set(Notice::getIsActive, isActive).set(Notice::getUpdateTime, new Date());
        //设置查询条件
        noticeLambdaUpdateWrapper.eq(Notice::getId, id);
        //调用NoticeService中的update方法
        boolean b = noticeService.update(noticeLambdaUpdateWrapper);
        if (!b) {
            return Result.error("切换公告失败");
        } else {
            return Result.success("切换公告成功");
        }
    }
    @Operation(summary = "根据ID获取公告启用状态")
    @GetMapping("/status/{id}")
    public Result<Boolean> getNoticeActiveStatus(
            @PathVariable @NotNull(message = "公告ID不能为空") Long id) {
            Notice notice = noticeService.getById(id);

//        Result<List<Notice>> activeNotices = noticeService.getActiveNotices();

        return Result.success(notice.getIsActive());


        }

}

















